﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace EMR
{
    public class PatientDal
    {
        public static int AddPatient(Patient patient)
        {
            string sql = "psp_insertPatient";
            int rowAffected = 0;
            try
            {
                rowAffected = SqlHelper.ExecuteNonQuery(sql, true, PatientDal.pms(patient));
            }
            catch
            {

            }
            return rowAffected;
        }

        public static int UpdatePatient(Patient patient, string beforeAdNo, string beforBedNo)
        {
            string sql = $@" UPDATE  tb_Patient
	                                    SET 
		                                AdmissionNo=@AdmissionNo ,BedNo=@BedNo ,Name=@Name ,Gender=@Gender,Department=@Department,
                                        VisitingStaff=@VisitingStaff,EnterTime=@EnterTime
		                                WHERE AdmissionNo ={beforeAdNo.Trim()} AND BedNo={beforBedNo.Trim()}";
            int rowAffected = 0;
            try
            {
                rowAffected = (int)SqlHelper.ExecuteNonQuery(sql, false, PatientDal.pms(patient));
            }
            catch
            {

            }
            return rowAffected;
        }



        public static SqlDataReader SelectPatient(string bedNo, string name, string AdNo)
        {
            string sql =
                "SELECT AdmissionNo,Name,Gender,BedNo,VisitingStaff,EnterTime,Department  " +
                "FROM tb_Patient  WHERE BedNo=@BedNo AND Name=@Name AND AdmissionNo=@AdmissionNo;";
            SqlParameter[] pms = new SqlParameter[]
            {
                new SqlParameter("@BedNo", bedNo),
                new SqlParameter("@Name", name),
                new SqlParameter("@AdmissionNo", AdNo),
            };
            return (SqlDataReader)SqlHelper.ExecuteQuery(sql, pms);
        }

        public static SqlDataReader SelectOne(string condition)
        {
            string sql =
                "SELECT AdmissionNo,Name,Gender,BedNo,VisitingStaff,EnterTime,Department  " +
                "FROM tb_Patient  WHERE BedNo like '%' + @BedNo +'%'  OR Name like '%' + @Name +'%' OR  AdmissionNo like '%' +  @AdmissionNo +'%' ;";
            SqlParameter[] pms = new SqlParameter[]
            {
                new SqlParameter("@BedNo", condition),
                new SqlParameter("@Name", condition),
                new SqlParameter("@AdmissionNo",condition),
            };
            return (SqlDataReader)SqlHelper.ExecuteQuery(sql, pms);
        }

        public static SqlDataReader SelectAll()
        {
            string sql = "SELECT  *  FROM tb_Patient AS P ";
            return (SqlDataReader)SqlHelper.ExecuteQuery(sql);
        }

        public static int DeletePatient(string bedNo, string name, string AdNo)
        {
            string sql = "DELETE  tb_Patient  WHERE BedNo=@BedNo AND Name=@Name AND AdmissionNo =@AdmissionNo;";
            SqlParameter[] pms = new SqlParameter[]
            {
                new SqlParameter("@BedNo", bedNo),
                new SqlParameter("@Name", name),
                new SqlParameter("@AdmissionNo", AdNo)
             };

            int rowAffected = 0;
            try
            {
                rowAffected = (int)SqlHelper.ExecuteNonQuery(sql, false, pms);
            }
            catch
            {

            }
            return rowAffected;
        }



        private static  SqlParameter[] pms(Patient patient)
        {
            SqlParameter[] pms = new SqlParameter[]
         {
                    new SqlParameter("@AdmissionNo", patient.AdmissionNo),
                    new SqlParameter("@BedNo", patient.BedNo),
                    new SqlParameter("@Name",patient.Name),
                    new SqlParameter("@Gender",patient.Gender.Trim()),
                    new SqlParameter("@Department",patient.Department),
                    new SqlParameter("@VisitingStaff",patient.VisitingStaff),
                    new SqlParameter("@EnterTime",patient.EnterTime)
         };
            return pms;
        }
        
    }
}
